<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Flask部署," />










<meta name="description" content="Flask项目部署项目生产环境Flask + mariadb + uwsgi + nginx12345678910111. 服务器系统Centos7.42.数据库mariadb   redis 3. 程序运行环境python3.x 由于Centos系统中自带的python版本是的2.x版本的.没有python3. 所以说需要我自己进行安装. 介绍了这么多就开始我们安装我们需要的软件和环境. 1.">
<meta name="keywords" content="Flask部署">
<meta property="og:type" content="article">
<meta property="og:title" content="Flask项目部署">
<meta property="og:url" content="http://yoursite.com/2018/06/23/Flask项目部署/index.html">
<meta property="og:site_name" content="Mr. Lee&#39;s blog">
<meta property="og:description" content="Flask项目部署项目生产环境Flask + mariadb + uwsgi + nginx12345678910111. 服务器系统Centos7.42.数据库mariadb   redis 3. 程序运行环境python3.x 由于Centos系统中自带的python版本是的2.x版本的.没有python3. 所以说需要我自己进行安装. 介绍了这么多就开始我们安装我们需要的软件和环境. 1.">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http://yoursite.com/2018/06/23/Flask项目部署/na.png">
<meta property="og:image" content="http://yoursite.com/2018/06/23/Flask项目部署/nginx.png">
<meta property="og:image" content="http://yoursite.com/2018/06/23/Flask项目部署/zong.png">
<meta property="og:image" content="http://yoursite.com/2018/06/23/Flask项目部署/nginx%E7%8A%B6%E6%80%81.png">
<meta property="og:image" content="http://yoursite.com/2018/06/23/Flask项目部署/%E6%95%B0%E6%8D%AE%E8%BF%81%E7%A7%BB.png">
<meta property="og:image" content="http://yoursite.com/2018/06/23/Flask项目部署/QIYI.png">
<meta property="og:image" content="http://yoursite.com/2018/06/23/Flask项目部署/aj.png">
<meta property="og:updated_time" content="2018-11-09T13:26:51.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Flask项目部署">
<meta name="twitter:description" content="Flask项目部署项目生产环境Flask + mariadb + uwsgi + nginx12345678910111. 服务器系统Centos7.42.数据库mariadb   redis 3. 程序运行环境python3.x 由于Centos系统中自带的python版本是的2.x版本的.没有python3. 所以说需要我自己进行安装. 介绍了这么多就开始我们安装我们需要的软件和环境. 1.">
<meta name="twitter:image" content="http://yoursite.com/2018/06/23/Flask项目部署/na.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2018/06/23/Flask项目部署/"/>





  <title>Flask项目部署 | Mr. Lee's blog</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Mr. Lee's blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description">知识改变命运,技术成就梦想</h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-commonweal">
          <a href="/404.html" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br />
            
            公益404
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off"
             placeholder="搜索..." spellcheck="false"
             type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/06/23/Flask项目部署/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Mr. Lee">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/avatar.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Mr. Lee's blog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">Flask项目部署</h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-06-23T22:16:10+08:00">
                2018-06-23
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  2,399
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  10
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h4 id="Flask项目部署"><a href="#Flask项目部署" class="headerlink" title="Flask项目部署"></a>Flask项目部署</h4><h5 id="项目生产环境Flask-mariadb-uwsgi-nginx"><a href="#项目生产环境Flask-mariadb-uwsgi-nginx" class="headerlink" title="项目生产环境Flask + mariadb + uwsgi + nginx"></a>项目生产环境Flask + mariadb + uwsgi + nginx</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">1. 服务器系统</span><br><span class="line"></span><br><span class="line">Centos7.4</span><br><span class="line"></span><br><span class="line">2.数据库</span><br><span class="line"></span><br><span class="line">mariadb   redis </span><br><span class="line"></span><br><span class="line">3. 程序运行环境</span><br><span class="line"></span><br><span class="line">python3.x</span><br></pre></td></tr></table></figure>
<p>由于Centos系统中自带的python版本是的2.x版本的.没有python3. 所以说需要我自己进行安装.</p>
<p>介绍了这么多就开始我们安装我们需要的软件和环境.</p>
<h6 id="1-安装python3"><a href="#1-安装python3" class="headerlink" title="1. 安装python3"></a>1. 安装python3</h6><p>首先需要安装python3的依赖包,可以复制下面的两行命令进行安装</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">yum -y groupinstall &quot;Development tools&quot;</span><br><span class="line"></span><br><span class="line">yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel</span><br></pre></td></tr></table></figure>
<p>安装好相关的依赖包之后要根据我们自己的项目需求选择对应的python3的版本,我这里使用的是python3.6.5的版本</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">使用wget命令下载我们的python3</span><br><span class="line"></span><br><span class="line">wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tar.xz</span><br></pre></td></tr></table></figure>
<p>我是是用的root账户登录的系统,所以默认的下载文件的地址是在[/root的目录].这样我们是用命令进入root目录找到我们python3.6.5程序使用xz命令进行文件的解压</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"># 解压文件</span><br><span class="line"></span><br><span class="line">xz -d Python-3.6.5.tar.xz</span><br><span class="line"></span><br><span class="line"># 解归档</span><br><span class="line"></span><br><span class="line">tar -xvf Python-3.6.5.tar</span><br></pre></td></tr></table></figure>
<p>切换至Python源代码目录并执行下面的命令进行配置和安装。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"># 切换到源代码目录</span><br><span class="line"></span><br><span class="line">cd Python-3.6.5</span><br><span class="line"></span><br><span class="line"># 配置</span><br><span class="line"></span><br><span class="line">./configure --prefix=/usr/local/python36 --enable-optimizations</span><br><span class="line"></span><br><span class="line"># 编译和安装</span><br><span class="line"></span><br><span class="line">make &amp;&amp; make install</span><br></pre></td></tr></table></figure>
<p>执行上面的三条命令,然后我们就要经历10-20分钟左右的等待(取决于机器的配置).执行完成后会看到Complete! 这样就表示我们的python3.6.5已经安装好.  但是有一个问题是到这里我们的python3.6.5还是不能运行的.我们需要做最后一步操作.建立软件了到系统的环境变量中去.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"># 建立python3的软连接</span><br><span class="line"></span><br><span class="line">ln -s /usr/local/python36/bin/python3 /usr/bin/python3</span><br><span class="line"></span><br><span class="line"># 建立pip3的软件链接</span><br><span class="line"></span><br><span class="line">ln -s /usr/local/python36/bin/pip3 /usr/bin/pip3</span><br></pre></td></tr></table></figure>
<p>执行了这两步操作我们的python3就可以正常的在Centos系统中正常运行了.</p>
<h6 id="2-安装Mariadb数据库"><a href="#2-安装Mariadb数据库" class="headerlink" title="2. 安装Mariadb数据库"></a>2. 安装Mariadb数据库</h6><p>安装命令. 同时安装mariadb的数据库和客户端程序</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum -y install mariadb mariadb-server</span><br></pre></td></tr></table></figure>
<p>安装完成后我们要启动mariadb数据库[有如下常用的命令]</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"># 启动数据库</span><br><span class="line"></span><br><span class="line">systemctl start mariadb</span><br><span class="line"></span><br><span class="line"># 重启数据库</span><br><span class="line"></span><br><span class="line">systemctl restart mariadb</span><br><span class="line"></span><br><span class="line"># 停止数据库</span><br><span class="line"></span><br><span class="line">systemctl stop mariadb</span><br><span class="line"></span><br><span class="line"># 查看数据库的运行状态</span><br><span class="line"></span><br><span class="line">systemctl status mariadb</span><br><span class="line"></span><br><span class="line"># 设置数据库开机自动启动</span><br><span class="line"></span><br><span class="line">systemctl enable mariadb</span><br></pre></td></tr></table></figure>
<p>启动好数据库后.由于我们是首次使用数据库我们需要配置数据库的初始密码</p>
<p>设置密码的命令是:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mysql_secure_installation</span><br></pre></td></tr></table></figure>
<p>这样就会进入到如下的页面,按照要求进行设置.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">Enter current password for root:&lt;–初次运行直接回车</span><br><span class="line"></span><br><span class="line">设置密码</span><br><span class="line"></span><br><span class="line">Set root password? [Y/n] &lt;– 是否设置root用户密码，输入y并回车或直接回车</span><br><span class="line"></span><br><span class="line">New password: &lt;– 设置root用户的密码</span><br><span class="line">Re-enter new password: &lt;– 再输入一次你设置的密码</span><br><span class="line"></span><br><span class="line">其他配置</span><br><span class="line"></span><br><span class="line">Remove anonymous users? [Y/n] &lt;– 是否删除匿名用户，回车</span><br><span class="line"></span><br><span class="line">Disallow root login remotely? [Y/n] &lt;–是否禁止root远程登录,回车,</span><br><span class="line"></span><br><span class="line">Remove test database and access to it? [Y/n] &lt;– 是否删除test数据库，回车</span><br><span class="line"></span><br><span class="line">Reload privilege tables now? [Y/n] &lt;– 是否重新加载权限表，回车</span><br></pre></td></tr></table></figure>
<p>初始化MariaDB完成，接下来测试登录的命令:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mysql -u root -p</span><br></pre></td></tr></table></figure>
<p>会提示我们数据密码,这个时候我们输入刚才设置的密码就可以登录到数据库中了.</p>
<p>因为项目需要远程连接数据库,数据库默认是没有开启的所以我们需要开启远程连接.在mysql数据库中的user表中可以看到默认是只能本地连接的，所有可以添加一个新的用户，该用户可以远程访问</p>
<ol>
<li><p>创建用户</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"># 先使用数据库</span><br><span class="line"></span><br><span class="line">use mysql;</span><br><span class="line"></span><br><span class="line"># 针对ip</span><br><span class="line">create user &apos;root&apos;@&apos;192.168.10.10&apos; identified by &apos;password&apos;;</span><br><span class="line"></span><br><span class="line">#全部</span><br><span class="line"> create user &apos;root&apos;@&apos;%&apos; identified by &apos;password&apos;;  [本项目选择的这条命令,password要替换成自己需要设置的root密码]</span><br></pre></td></tr></table></figure>
</li>
<li><p>授权</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"># 给用户最大权限  [本项目选择这条命令,password要替换成自己需要设置的root密码]</span><br><span class="line">grant all privileges on *.* to &apos;root&apos;@&apos;%&apos; identified by &apos;password&apos;;</span><br><span class="line"></span><br><span class="line"># 给部分权限(test 数据库)</span><br><span class="line"></span><br><span class="line">grant all privileges on test.* to &apos;root&apos;@&apos;%&apos; identified by &apos;password&apos; with grant option;</span><br><span class="line"></span><br><span class="line"># 刷新权限表</span><br><span class="line">flush privileges;</span><br><span class="line"></span><br><span class="line"># 查看</span><br><span class="line">show grants for &apos;root&apos;@&apos;localhost&apos;;</span><br></pre></td></tr></table></figure>
<p>接下来就可以在远程的数据库可视化工具中直接访问该服务器中的mysql了。</p>
<p>本项目用的是Nacicat</p>
<p><img src="/2018/06/23/Flask项目部署/na.png" alt=""></p>
<p>​</p>
</li>
</ol>
<h6 id="3-安装项目需要使用的虚拟环境"><a href="#3-安装项目需要使用的虚拟环境" class="headerlink" title="3. 安装项目需要使用的虚拟环境"></a>3. 安装项目需要使用的虚拟环境</h6><ol>
<li><p>安装virtualenv</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum install python-virtualenv</span><br></pre></td></tr></table></figure>
</li>
<li><p>创建虚拟环境</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">virtualenv --no-site-packages ajenv</span><br><span class="line"></span><br><span class="line">cd ajenv</span><br><span class="line"></span><br><span class="line"># 激活虚拟环境</span><br><span class="line">source bin/activate</span><br></pre></td></tr></table></figure>
</li>
<li><p>安装环境需要的包</p>
<p>项目文件夹下面自己建立了有一个requirement文件夹下面有一个re_install的文件.就是本项目需要用到的包.这样我们一次性就能全部安装好.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">pip3 install -r re_install.txt</span><br><span class="line"></span><br><span class="line">其中re_install.txt文件中记录的是需要安装包的名称以及对应的版本</span><br></pre></td></tr></table></figure>
<p>​</p>
</li>
</ol>
<h5 id="部署"><a href="#部署" class="headerlink" title="部署"></a>部署</h5><p>前面三个准备工作做好了这样我们就可以放心的做我们部署工作了.</p>
<h6 id="1-安装nginx服务器"><a href="#1-安装nginx服务器" class="headerlink" title="1. 安装nginx服务器"></a>1. 安装nginx服务器</h6><ol>
<li><p>添加nginx存储库</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum install epel-release</span><br></pre></td></tr></table></figure>
</li>
<li><p>安装nginx</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum install nginx</span><br></pre></td></tr></table></figure>
</li>
<li><p>运行启动nginx</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">systemctl start nginx</span><br></pre></td></tr></table></figure>
<p>常用的nginx运行命令</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">#查看nginx的状态</span><br><span class="line">systemctl status nginx </span><br><span class="line"># 启动</span><br><span class="line">systemctl start nginx</span><br><span class="line"># 停止关闭</span><br><span class="line">systemctl stop nginx</span><br><span class="line"># 设置开机启动</span><br><span class="line">systemctl enable nginx</span><br><span class="line"># 禁止开机启动</span><br><span class="line">systemctl disable nginx</span><br></pre></td></tr></table></figure>
<p>需要检查一下如果您正在运行防火墙，请运行以下命令以允许HTTP和HTTPS通信：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">sudo firewall-cmd --permanent --zone=public --add-service=http </span><br><span class="line"></span><br><span class="line">sudo firewall-cmd --permanent --zone=public --add-service=https</span><br><span class="line"></span><br><span class="line">sudo firewall-cmd --reloa</span><br></pre></td></tr></table></figure>
<p>这些都弄好了我们就可以输入服务器的ip地址到浏览器中看到如下界面:</p>
</li>
</ol>
<p><img src="/2018/06/23/Flask项目部署/nginx.png" alt="gin"></p>
<h6 id="2-安装uwsgi"><a href="#2-安装uwsgi" class="headerlink" title="2. 安装uwsgi"></a>2. 安装uwsgi</h6><ol>
<li><p>安装uwsgi  (因为我们是python3的环境所以我们一定要使用pip3来安装,否则就安装到python2中去了.)</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pip3 install uwsgi</span><br></pre></td></tr></table></figure>
</li>
<li><p>将uwsgi配置到环境变量中,建立软链接</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ln -s /usr/local/python36/bin/uwsgi /usr/bin/uwsgi</span><br></pre></td></tr></table></figure>
</li>
</ol>
<h6 id="3-配置项目代码-配置项目nginx-配置uwsgi-ini等"><a href="#3-配置项目代码-配置项目nginx-配置uwsgi-ini等" class="headerlink" title="3. 配置项目代码,配置项目nginx.配置uwsgi.ini等"></a>3. 配置项目代码,配置项目nginx.配置uwsgi.ini等</h6><p>本案例的配置文件，都习惯将每一个项目的配置文件，日志文件，虚拟环境放在一起，这样开发方便，运维也方便维护.项目的目录结构如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">[root@izm5eifctth7468z9e6fz0z home]#</span><br><span class="line"></span><br><span class="line">conff  envf  logf  srcf</span><br></pre></td></tr></table></figure>
<p>由于我的系统中配置过其他项目.这个项目我重新命名了如上面的四个文件夹.其中：</p>
<p>conff是配置文件，用于存放项目的nginx.conf文件，uwsgi.ini文件</p>
<p>logf是日志文件，用于存放nginx的启动成功和失败文件，以及uwsgi的运行日志文件</p>
<p>envf是用于存放虚拟环境</p>
<p>srcf是项目文件，该目录下上传的是目录代码</p>
<h6 id="4-配置nginx-conf文件"><a href="#4-配置nginx-conf文件" class="headerlink" title="4. 配置nginx.conf文件"></a>4. 配置nginx.conf文件</h6><p>首先进入conff文件夹中创建一个名称为ajnginx.conf 的文件.</p>
<p>每一个项目对应有一个自己定义的nginx的配置文件，比如爱家项目，我定义为ajnginx.conf文件.具体配置内容如下:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">server &#123;</span><br><span class="line">    listen       80;</span><br><span class="line">    server_name 47.104.205.141 localhost;</span><br><span class="line"></span><br><span class="line">    access_log /home/logf/access.log;</span><br><span class="line">    error_log /home/logf/error.log;</span><br><span class="line"></span><br><span class="line">    location / &#123;</span><br><span class="line">        include uwsgi_params;</span><br><span class="line">        uwsgi_pass 127.0.0.1:8890;</span><br><span class="line"></span><br><span class="line">        uwsgi_param UWSGI_CHDIR /home/srcf/Aj;</span><br><span class="line">        uwsgi_param UWSGI_SCRIPT manage:app;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>说明: </p>
<p>listen — 表示监听的端口</p>
<p>server_name — 表示服务名称(有域名可以换成域名)</p>
<p>access_log   — nginx成功的文件日志地址</p>
<p>error_log   — nginx失败了的错误日志文件地址</p>
<p>location — 接收请求</p>
<p>include uwsgi_params — 固定写法</p>
<p>uwsgi_param UWSGI_SCRIPT manage:app;   # 启动flask的文件:Flask的实例</p>
<p>uwsgi_param UWSGI_CHDIR 项目的目录</p>
</blockquote>
<p>这个配置文件写好之后我们需要修改nginx总的配置文件.这个文件在:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vim /etc/nginx/nginx.conf</span><br></pre></td></tr></table></figure>
<p>修改如下图的位置:</p>
<p><img src="/2018/06/23/Flask项目部署/zong.png" alt="on"></p>
<p>修改完成之后我们需要重新启动nginx服务器</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">systemctl restart nginx</span><br></pre></td></tr></table></figure>
<p>看到如下界面表示我们nginx服务器已经正常启动了</p>
<p><img src="/2018/06/23/Flask项目部署/nginx%E7%8A%B6%E6%80%81.png" alt="ginx状"></p>
<h6 id="5-配置uwsgi文件"><a href="#5-配置uwsgi文件" class="headerlink" title="5. 配置uwsgi文件"></a>5. 配置uwsgi文件</h6><p>在conff文件夹下在定义一个名称为uwsgi.ini文件.写入如下的配置信息</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">[uwsgi]</span><br><span class="line"></span><br><span class="line">socket=127.0.0.1:8890</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">chdir=/home/srcf/Aj</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">pythonpath=/usr/local/python36/bin/python3</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">pythonhome=/home/envf/ajenv</span><br><span class="line"></span><br><span class="line">callbale=app</span><br><span class="line"></span><br><span class="line">logto = /home/logf/uwsgi.log</span><br></pre></td></tr></table></figure>
<blockquote>
<p>说明:</p>
<p>[uwsgi] — 固定写法必须要有</p>
<p>socket — 要与nginx中uwsgi_pass一致</p>
<p>chdir — 项目所在的目录</p>
<p>pythonpath — python3所在的目录</p>
<p>pythonhome — 项目虚拟环境的目录</p>
<p>logto — 项目日志文件位置</p>
</blockquote>
<p>这里配置完成就可以开始启动我们的项目了.</p>
<h6 id="6-迁移我们的数据库"><a href="#6-迁移我们的数据库" class="headerlink" title="6. 迁移我们的数据库"></a>6. 迁移我们的数据库</h6><p><img src="/2018/06/23/Flask项目部署/%E6%95%B0%E6%8D%AE%E8%BF%81%E7%A7%BB.png" alt="据迁"></p>
<p><img src="/2018/06/23/Flask项目部署/QIYI.png" alt="IY"></p>
<p>运行项目必须进入到conff文件夹下面,运行的命令是:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">uwsgi --ini uwsgi.ini</span><br></pre></td></tr></table></figure>
<p>结果:</p>
<p><img src="/2018/06/23/Flask项目部署/aj.png" alt=""></p>
<p>多个网站的可以参考文章《<a href="https://leezhonglin.github.io/2018/11/09/nginx-uwsgi%E6%88%96nginx-python%E4%B8%ADmanage%E9%83%A8%E7%BD%B2%E5%A4%9A%E4%B8%AA%E7%BD%91%E7%AB%99/" target="_blank" rel="noopener">nginx+uwsgi或nginx+python中manage部署多个网站</a>》</p>
<p>原创出品，欢迎转载收藏。</p>
<blockquote>
<p>作者：lizhonglin</p>
<p><strong>github:</strong> <a href="https://github.com/Leezhonglin/" target="_blank" rel="noopener">https://github.com/Leezhonglin/</a></p>
<p><strong>blog:</strong> <a href="https://leezhonglin.github.io/" target="_blank" rel="noopener">https://leezhonglin.github.io/</a></p>
</blockquote>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div></div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/WechatIMG26.jpeg" alt="Mr. Lee 微信支付"/>
        <p>微信支付</p>
      </div>
    

    

    

  </div>
</div>

      </div>
    

    

    <div>
      
        <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">
            -------------本文结束
            <i class="fa fa-paw"></i>
            感谢您的阅读-------------
        </div>
    
</div>
      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Flask部署/" rel="tag"><i class="fa fa-tag"></i> Flask部署</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/06/18/初学者的flask项目/" rel="next" title="初学者的flask项目">
                <i class="fa fa-chevron-left"></i> 初学者的flask项目
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/06/30/Ajax写法/" rel="prev" title="Ajax写法">
                Ajax写法 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/avatar.png"
                alt="Mr. Lee" />
            
              <p class="site-author-name" itemprop="name">Mr. Lee</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">33</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">30</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/Leezhonglin" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:380604322@qq.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
            </div>
          

          
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-block">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                友情链接
              </div>

              <ul class="links-of-blogroll-list">

                
                    <span class="links-of-author-item" style="text-align:center">
                      <a href="https://my.csdn.net/jackfrued/" title="大 神" target="_blank">
                        大 神
                      </a>
                    </span>
                
                    <span class="links-of-author-item" style="text-align:center">
                      <a href="https://blog.csdn.net/qq_33196814" title="CSDN" target="_blank">
                        CSDN
                      </a>
                    </span>
                
                    <span class="links-of-author-item" style="text-align:center">
                      <a href="http://www.python.org" title="python" target="_blank">
                        python
                      </a>
                    </span>
                
                    <span class="links-of-author-item" style="text-align:center">
                      <a href="http://redisdoc.com/" title="redis" target="_blank">
                        redis
                      </a>
                    </span>
                

              </ul>


            </div>
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-4"><a class="nav-link" href="#Flask项目部署"><span class="nav-number">1.</span> <span class="nav-text">Flask项目部署</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#项目生产环境Flask-mariadb-uwsgi-nginx"><span class="nav-number">1.1.</span> <span class="nav-text">项目生产环境Flask + mariadb + uwsgi + nginx</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#1-安装python3"><span class="nav-number">1.1.1.</span> <span class="nav-text">1. 安装python3</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#2-安装Mariadb数据库"><span class="nav-number">1.1.2.</span> <span class="nav-text">2. 安装Mariadb数据库</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#3-安装项目需要使用的虚拟环境"><span class="nav-number">1.1.3.</span> <span class="nav-text">3. 安装项目需要使用的虚拟环境</span></a></li></ol></li><li class="nav-item nav-level-5"><a class="nav-link" href="#部署"><span class="nav-number">1.2.</span> <span class="nav-text">部署</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#1-安装nginx服务器"><span class="nav-number">1.2.1.</span> <span class="nav-text">1. 安装nginx服务器</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#2-安装uwsgi"><span class="nav-number">1.2.2.</span> <span class="nav-text">2. 安装uwsgi</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#3-配置项目代码-配置项目nginx-配置uwsgi-ini等"><span class="nav-number">1.2.3.</span> <span class="nav-text">3. 配置项目代码,配置项目nginx.配置uwsgi.ini等</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#4-配置nginx-conf文件"><span class="nav-number">1.2.4.</span> <span class="nav-text">4. 配置nginx.conf文件</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#5-配置uwsgi文件"><span class="nav-number">1.2.5.</span> <span class="nav-text">5. 配置uwsgi文件</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#6-迁移我们的数据库"><span class="nav-number">1.2.6.</span> <span class="nav-text">6. 迁移我们的数据库</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Mr. Lee</span>

  
</div>


  <span class="post-meta-divider">|</span>



  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://github.com/Leezhonglin">Leezhonglin</a> 提供技术支持</div>



  <span class="post-meta-divider">|</span>


<div class="powered-by">
<i class="fa fa-user-md"></i><span id="busuanzi_container_site_uv">
  本站访客数:<span id="busuanzi_value_site_uv"></span>
</span>


  <span class="post-meta-divider">|</span>


<span id="busuanzi_container_site_pv">
    本站总访问量:<span id="busuanzi_value_site_pv"></span>次
</span>


  <span class="post-meta-divider">|</span>


</div>


<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">博客全站共:64.5k字</span>
</div>


  <span class="post-meta-divider">|</span>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  
  

  

  

  

</body>
</html>
